package tn.softcom.dfbuilder.modele;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import tn.softcom.dfbuilder.Utils;

public class ADJICellsGroup {

	private String source;
	private GpsData gpsData;
	private List<ADJICells> lstADJI;
	private List<Integer> freePosition;
	private List<ADJICells> lstADJIFailToGetID ;
	private List<RapportADJI> lstRapport;
	public ADJICellsGroup() {
	}

	public ADJICellsGroup(String source) {
		super();
		this.source = source;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public List<ADJICells> getLstADJI() {
		return lstADJI;
	}

	public void setLstADJI(List<ADJICells> lstADJI) {
		this.lstADJI = lstADJI;
	}

	public void addADJI(ADJICells ADJI) {
		if (lstADJI == null)
			lstADJI = new ArrayList<ADJICells>();
		lstADJI.add(ADJI);
	}
	
	public void addRapport(RapportADJI rapport) {
		if (lstRapport == null)
			lstRapport = new ArrayList<RapportADJI>();
		lstRapport.add(rapport);
	}
	
	public void addADJIFail(ADJICells ADJI) {
		if (lstADJIFailToGetID == null)
			lstADJIFailToGetID = new ArrayList<ADJICells>();
		lstADJIFailToGetID.add(ADJI);
	}

	private void setFreePosition() {
		try {
			Connection conn = Utils.getConnection();
			Statement s = conn.createStatement();

			
			String queryCount = "SELECT Count(a.ADJIId) FROM A_ADJI a, A_WCEL b where a.RncId = b.RncId and a.WBTSId = b.WBTSId and a.LcrId = b.LcrId and b.name = '"+source+"'";
			s.execute(queryCount);
			ResultSet rsqueryCount = s.getResultSet();
			int count = 0;;
			if ((rsqueryCount != null) && (rsqueryCount.next())) {
				count = rsqueryCount.getInt(1);
			}
			// create a table
			String query = "SELECT a.* FROM A_ADJI a, A_WCEL b where a.RncId = b.RncId and a.WBTSId = b.WBTSId and a.LcrId = b.LcrId and b.name = '"+source+"'";
			s.execute(query);
			ResultSet rs = s.getResultSet();
			freePosition = new ArrayList<Integer>();
			int free = 32 - count;
			int tab [] = new int[32];
			if(free > 0){
				while ((rs != null) && (rs.next())) {
					int id = Integer.valueOf(rs.getString("ADJIId")) ;
					tab [id] = id;
				}
				for(int i = 0;i<32; i++){
					if(tab[i] == 0)
						freePosition.add(i);
				}
			}
			
			// close and cleanup
			s.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void setADJIID(){
		setFreePosition() ;
		if(freePosition!=null && freePosition.size()>0){
			if(freePosition.size()>=lstADJI.size()){
				int i = 0;
				for(ADJICells ADJI:lstADJI){
					ADJI.setADJIId(""+freePosition.get(i));
					i++;
				}
			}else{
				for(int i =0;i<freePosition.size();i++){
					lstADJI.get(i).setADJIId(""+freePosition.get(i));
				}
				
				for(int i = freePosition.size();i<lstADJI.size();i++){
					addADJIFail((ADJICells) lstADJI.get(i).clone());
					lstADJI.get(i).setADJIId("MAX");
				}
			}
		}else{
			for(int i = 0;i<lstADJI.size();i++){
				addADJIFail((ADJICells) lstADJI.get(i).clone());
				lstADJI.get(i).setADJIId("MAX");
			}
		}
		
	}
	
	
	public RapportADJI retreiveNewId(){
		String id = "";
		RapportADJI rt = new RapportADJI();
		while(lstRapport != null && lstRapport.size()>0 && "".equals(id)){
			Collections.sort(lstRapport);
			System.out.println("rapport size = " +lstRapport);
			RapportADJI rapport = lstRapport.get(0);
			try {
				Connection conn = Utils.getConnection();
				Statement s = conn.createStatement();
				String query = "SELECT a.ADJIId FROM A_ADJI a, A_WCEL wc where a.RncId = wc.RncId and a.WBTSId = wc.WBTSId and a.LcrId = wc.LcrId and wc.name = '"+rapport.getSource_WCEL_Name()+"' and a.name = '"+rapport.getTarget_WCEL_Name()+"' and wc.AdminCellState =1";
				s.execute(query);
				ResultSet rs = s.getResultSet();
				if(rs != null && (rs.next())) {
					 id = rs.getString("ADJIId") ;
					 rt = (RapportADJI) rapport.clone();
					 rt.setIdToRetreve(id);
				}
				
				s.close();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			lstRapport.remove(0);
		}
		return rt;
	}
	

	public List<ADJICells> getLstADJIFailToGetID() {
		return lstADJIFailToGetID;
	}

	public List<RapportADJI> getLstRapport() {
		return lstRapport;
	}


	public GpsData getGpsData() {
		return gpsData;
	}

	public void setGpsData(GpsData gpsData) {
		this.gpsData = gpsData;
	}
}
